package com.boom.monitor.util;

import cn.hutool.core.date.DateUtil;
import com.boom.common.utils.StringUtils;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * @author:yzw
 * @Date:2021/3/1 下午4:33
 * @desc:
 * @version:1.0
 */
public class EntryUtils {

	/**
	 * 利用反射设值
	 *
	 * @param t
	 * @param fieldName
	 * @param value
	 * @return T
	 * @author yzw
	 * @time 2021/3/31 下午5:03
	 */
	public static <T> T setValue(T t, String fieldName, Object value) {
		try {
			Field field = t.getClass().getDeclaredField(fieldName);
			field.setAccessible(true);
			String size = "Size";
			String startTime = "startTime";
			if (fieldName.endsWith(size)) {
				value = FormatterUnitUtils.formatterUnit((Long) value);
			} else if (StringUtils.equals(fieldName, startTime)) {
				value = DateUtil.format(new Date((Long) value), "yyyy-MM-dd HH:mm:ss");
			}
			field.set(t, value);
			return t;
		} catch (Exception e) {
			return t;
		}
	}

}
